// @ts-nocheck

import {API_BASE_URL,API_BASE_WEAPONSPAGESIZE} from  "@/lib/config";
/** @type {import('./$types').PageServerLoad} */
export async function load({ fetch, params,url }) {


    let currentPage = Number(params.slug) || 1;
    let name = url.searchParams.get('name') || '';
    let damage = url.searchParams.get('damage') || '';
    let tier = url.searchParams.get('tier') || '';
    let type = url.searchParams.get('type') || '';
    let ammo = url.searchParams.get('ammo') || '';
    let craft = url.searchParams.get('craft') || '';

    let options = {
        table_name:"DestinyInventoryItemDefinition",
        count:API_BASE_WEAPONSPAGESIZE,
        page:currentPage,
        filter_dict:{
            keys: [
                "stats.stats.2961396640.value",
                "itemTypeDisplayName",
                "stats.stats.447667954.value",
                "displayProperties.name",
                "hash",
                "stats.stats.4284893193.value"
            ],
            filters: {
                itemType:3,
                ...(damage?{damageTypes:[damage]}:{}),
                ...(tier?{"inventory.tierType":tier}:{}),
                ...(type?{itemCategoryHashes:[type]}:{}),
                ...(ammo?{"equippingBlock.ammoType":Number(ammo)}:{}),
                
            },
            advanced_filter: {
                ...(craft?{can_craft:craft==1?true:false}:{}),
                standard_item: true
            },
            search_list:[name]
        },
        language:'zh-chs',
        is_fuzzy:true,
        priority_sort:false
    }
    
    //获取全部武器
    const res = await fetch(`${API_BASE_URL}/api/search`,{
        method:"POST",
        body:JSON.stringify(options), 
        headers:{
            'Content-Type':'application/json'
        }
    });
	const {Response} = await res.json();

    //拼接参数
    let qureyParams = '?'
    for(let i = 0; i < Response.list.length; i++){
        qureyParams += `weapon_hash_list=${Response.list[i]}${i == Response.list-1?'':'&'}`
    }
    let perkRes = await fetch(`${API_BASE_URL}/api/weapon_multi${qureyParams}`);
    let {Response:perkItems} = await perkRes.json();


    if(Response){
        return {
            code:0,
            total:Response.num,
            base:Object.values(perkItems)
        };
    }
    return {
        code:1,
    };
	
	
}


